<?php
namespace app\index\controller;

use think\Controller;
use think\Session;
use think\Db;
use app\index\controller\Util;

class Wechatpush extends Controller {
	//web端推送
	public function getData() {
		Vendor('phpqrcode');
		$result = Session::get('equnique');
		$util = new Util();
		if ($result) {
			$info = "成功获取所发布的地震,Web端推送成功！";
			$util->sendLogInfo($info);
			return $result;
		} else {
			$info = "未能获取所发布的地震,Web端推送失败！";
			$util->sendLogInfo($info);
			return null;
		}
	}

	//手机端推送
	public function getPhoneData() {
		Vendor('phpqrcode');
		$result = Session::get('equnique');
		$util = new Util();
		if ($result) {
			$cip = $this->getIP();
			// $url = 'http://'.$cip.':8080'.'/#/home/'.$result;
			$url = 'http://172.17.130.182:8080/#/home/' . $result;
			// return response($enc = \QRcode::png($url, false, 'L', 5, 2), 200)->contentType("image/png");
			$info = "成功获取所发布的地震,手机端推送成功！";
			$util->sendLogInfo($info);
			return $url;
		} else {
			$info = "未能获取所发布的地震,手机端推送失败";
			$util->sendLogInfo($info);
			return '未能获取所发布的地震,推送失败!';
		}
	}

	//自动截图（暂时舍弃）
	public function autoshort() {
		$cip = $this->getIP();
		// $url = 'http://'.$cip.':8080/#/home/';
		// $url = 'http://localhost:8080/#/home/';
		$result = Session::get('equnique');
		echo "开始截图";
		if ($result) {
			if(!is_dir('D:\\wamp64\\www\\subaoApi\\public\\static\\uploads\\image\\' . $result))
				mkdir('D:\\wamp64\\www\\subaoApi\\public\\static\\uploads\\image\\' . $result, 0777, true);
			// $myfile = fopen("newfile.bat", "w") or die("Unable to open file!");
			// $txt = "python ..\public\auto.py {$url} {$result}";
			// fwrite($myfile, $txt);
			// fclose($myfile);
			exec("node server.js");
			// echo "截图完成";
		} else {
			return '未能获取所发布的地震,推送失败!';
		}
	}

	//微信推送
	public function wechartPush() {
		$result = Session::get('equnique');
		$Dbdata=Db::table('published_seismic_database')->field("abvProvince as district, EQTTIME as time, LONGITUDE as lon, LATITUDE as lat, MAGNITUDE as magn, Intensity as intensity, DEPTH as depth")->where("ID = '" . $result . "'")->select();
		$util = new Util();

		$data = array(
			 // 模板消息内容，根据模板详情进行设置
				'addres'    =>$Dbdata[0]['district'],
				'longtitude' => $Dbdata[0]['lon'],
				'lattitude' => $Dbdata[0]['lat'],
				'datatime' => $Dbdata[0]['time'],
				'magnitude' => $Dbdata[0]['magn'],
				'url' =>  'http://172.17.130.182:8080/#/home/' . $result
			);
 
		$value = ['appid' => 'wx5957c95a261f6e8a','appsecret' => 'c47a5129aba06dc2fc161b4931503568'];
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$value['appid']."&secret=".$value['appsecret'];
		if (cookie('access_token')){
			$access_token=cookie('access_token');
		}else{
			$cont = json_decode($this->getToken($url));
			$access_token=$cont->access_token;
			setcookie('access_token',$access_token,7200);
		}//获取access_token
 
		$all = $this->get_allopenid($access_token);//获取关注的用户，因为只能推送关注的用户。
		$openids = $all['data']['openid'];//关注用户的openID
		$openids = array_values($openids);//这里是筛选没有退订的用户，并重新排序键值对。这些推送最好是可以退订的，不然用户举报可能会封号哦。我是根据用户回复“退订”关键词来标记的。这一步是有必要的。
	
 
		foreach ($openids as $key => $value) {
			$params = json_encode($this->json_tempalte($value,$data),JSON_UNESCAPED_UNICODE);
			$fp = fsockopen('api.weixin.qq.com', 80, $error, $errstr, 1);
			var_dump($error);
			$http = "POST /cgi-bin/message/template/send?access_token={$access_token} HTTP/1.1\r\nHost: api.weixin.qq.com\r\nContent-type: application/x-www-form-urlencoded\r\nContent-Length: " . strlen($params) . "\r\nConnection:close\r\n\r\n$params\r\n\r\n";
			fwrite($fp, $http);
			$resp_str='';
			while(!feof($fp)) {
				$resp_str .= fgets($fp,512);//返回值放入$resp_str
			}
			var_dump($resp_str);
			fclose($fp);
			sleep(1);
		}//这里就是遍历推送到各个openID了、sleep（1）是为了防止推送过快出问题。
		$info = "成功获取所发布的地震,微信推送成功！";
		$util->sendLogInfo($info);
	}
	protected function getToken($url) {//这个方法是获取access_token
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko)");
		curl_setopt($ch, CURLOPT_ENCODING, "gzip");
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		$output = curl_exec($ch);
		curl_close($ch);
		return $output;
	}
	protected function get_allopenid($access_token,$next_openid = '') {//这个方法是获取所有关注的用户的
		$url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token={$access_token}&next_openid=".$next_openid;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
		$data = curl_exec($ch);
		$data = json_decode($data,true);
		if ($data["count"] == 10000){
			$newdata = $this->get_allopenid($access_token,$data["next_openid"]);
			$data["data"]["openid"] = array_merge_recursive($date["data"]["openid"], $newdata["data"]["openid"]);
		}
		return $data;
	}
	protected function json_tempalte($openid,$data,$template_id = '1kHpyqPFgmfn5h6uA2164z78gJAWXgVWkI0qh2cTL38') {//这里是组装模版消息的方法
		// var_dump($data);
		$template=array(
			'touser'=>$openid,
			'template_id'=>$template_id,//这个是模版ID，获取方法自己看文档很详细。
	
			'url'=>$data['url'],
			'topcolor'=>"#7B68EE",
			'data'=>array(
				'addres'=>array('value'=>$data['addres'],'color'=>"#000"),
				'longtitude'=>array('value'=>$data['longtitude'],'color'=>'#F70997'),
				'lattitude'=>array('value'=>$data['lattitude'],'color'=>'#248d24'),
				'datatime'=>array('value'=>date("Y-m-d H:i:s"),'color'=>'#000'),
				'magnitude'  =>array('value'=>$data['magnitude'],'color'=>'#1784e8'),
				'url'=>array('value'=>$data['url'],'color'=>'#1784e8') )
		);
		return $template;
	}

	//获取操作系统为win2000/xp、win7的本机IP真实地址
	protected function getIP() {
		$preg = "/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\Z/";
		exec("ipconfig", $out, $stats);
		if (!empty($out)) {
			foreach ($out as $row) {
				if (strstr($row, "IP") && strstr($row, ":") && !strstr($row, "IPv6")) {
					$tmpIp = explode(":", $row);
					if (preg_match($preg, trim($tmpIp[1]))) {
						return trim($tmpIp[1]);
					}
				}
			}
		}
	}

}